Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ModelingTookit extension #280

Merged
merged 4 commits into from
Feb 1, 2024
Merged

Fix ModelingTookit extension #280

merged 4 commits into from
Feb 1, 2024

Conversation

TorkelE
Copy link
Member

@TorkelE TorkelE commented Feb 1, 2024

This is an attempt to fix the modelling toolkit extension, and reintroduce support for the mtk_to_si function, which was removed in the last update.

Changes:

  • mtk_to_si is no longer exported from the extension. But instead created in the main package (without methods) and methods then added in the extension.
  • I have removed the export statements in the extension as these should not be there.
  • I have renamed the extension to ModelingToolkitSIExt. Motivation: extensions should (ideally) have names unique to both packages involved. If two packages both created extensions called ModelingToolkitExt (no unthinkable), these will overwrite each other, causing errors if loaded simultaneously.

Unfortunately this does not work fully. This code:

using StructuralIdentifiability, ModelingToolkit

@parameters r1, r2, c1, c2, beta1, beta2, chi1, chi2
@variables t, x1(t), x2(t), y(t), u(t)

D= Differential(t)

eqs = [
    D(x1) ~ r1 * x1 * (1 - c1 * x1) + beta1 * x1 * x2 / (chi1 + x2) + u,
    D(x2) ~ r2 * x2 * (1 - c2 * x2) + beta2 * x1 * x2 / (chi2 + x1),
]

measured_quantities = [y ~ x1]

ode_mtk = ODESystem(eqs, t, name = :mutualist)

local_id_1 = assess_identifiability(ode_mtk, measured_quantities = measured_quantities)
global_id_1 = assess_local_identifiability(ode_mtk, measured_quantities = measured_quantities)


si_model = mtk_to_si(ode_mtk, measured_quantities)
global_id_2 = assess_identifiability(si_model)

generates a

ERROR: MethodError: no method matching assess_identifiability(::Tuple{ODE{Nemo.QQMPolyRingElem}, Dict{SymbolicUtils.BasicSymbolic{…}, Nemo.QQMPolyRingElem}})

Closest candidates are:
  assess_identifiability(::ODESystem; measured_quantities, funcs_to_check, prob_threshold, loglevel)
   @ ModelingToolkitSIExt ~/.julia/dev/StructuralIdentifiability/ext/ModelingToolkitSIExt.jl:349
  assess_identifiability(::ODE{P}; funcs_to_check, prob_threshold, loglevel) where P<:AbstractAlgebra.MPolyRingElem{Nemo.QQFieldElem}
   @ StructuralIdentifiability ~/.julia/dev/StructuralIdentifiability/src/StructuralIdentifiability.jl:98

Stacktrace:
 [1] top-level scope
   @ ~/Desktop/Julia Playground/Catalyst playground/Environment - Temporary/temproary_playground.jl:24
Some type information was truncated. Use `show(err)` to see complete types.

error.

This, however, works:

ode = @ODEmodel(
    x1'(t) = -(a01 + a21) * x1(t) + a12 * x2(t) + u(t),
    x2'(t) = a21 * x1(t) - a12 * x2(t) - x3(t) / b,
    x3'(t) = x3(t),
    y(t) = x2(t)
)
assess_identifiability(ode)

Copy link

codecov bot commented Feb 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (3cfc392) 89.81% compared to head (18d3647) 89.81%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #280   +/-   ##
=======================================
  Coverage   89.81%   89.81%           
=======================================
  Files          27       27           
  Lines        3268     3268           
=======================================
  Hits         2935     2935           
  Misses        333      333           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pogudingleb
Copy link
Collaborator

This is an attempt to fix the modelling toolkit extension, and reintroduce support for the mtk_to_si function, which was removed in the last update.

Changes:

  • mtk_to_si is no longer exported from the extension. But instead created in the main package (without methods) and methods then added in the extension.
  • I have removed the export statements in the extension as these should not be there.
  • I have renamed the extension to ModelingToolkitSIExt. Motivation: extensions should (ideally) have names unique to both packages involved. If two packages both created extensions called ModelingToolkitExt (no unthinkable), these will overwrite each other, causing errors if loaded simultaneously.

Unfortunately this does not work fully. This code:

using StructuralIdentifiability, ModelingToolkit

@parameters r1, r2, c1, c2, beta1, beta2, chi1, chi2
@variables t, x1(t), x2(t), y(t), u(t)

D= Differential(t)

eqs = [
    D(x1) ~ r1 * x1 * (1 - c1 * x1) + beta1 * x1 * x2 / (chi1 + x2) + u,
    D(x2) ~ r2 * x2 * (1 - c2 * x2) + beta2 * x1 * x2 / (chi2 + x1),
]

measured_quantities = [y ~ x1]

ode_mtk = ODESystem(eqs, t, name = :mutualist)

local_id_1 = assess_identifiability(ode_mtk, measured_quantities = measured_quantities)
global_id_1 = assess_local_identifiability(ode_mtk, measured_quantities = measured_quantities)


si_model = mtk_to_si(ode_mtk, measured_quantities)
global_id_2 = assess_identifiability(si_model)

generates a

ERROR: MethodError: no method matching assess_identifiability(::Tuple{ODE{Nemo.QQMPolyRingElem}, Dict{SymbolicUtils.BasicSymbolic{…}, Nemo.QQMPolyRingElem}})

Closest candidates are:
  assess_identifiability(::ODESystem; measured_quantities, funcs_to_check, prob_threshold, loglevel)
   @ ModelingToolkitSIExt ~/.julia/dev/StructuralIdentifiability/ext/ModelingToolkitSIExt.jl:349
  assess_identifiability(::ODE{P}; funcs_to_check, prob_threshold, loglevel) where P<:AbstractAlgebra.MPolyRingElem{Nemo.QQFieldElem}
   @ StructuralIdentifiability ~/.julia/dev/StructuralIdentifiability/src/StructuralIdentifiability.jl:98

Stacktrace:
 [1] top-level scope
   @ ~/Desktop/Julia Playground/Catalyst playground/Environment - Temporary/temproary_playground.jl:24
Some type information was truncated. Use `show(err)` to see complete types.

error.

This, however, works:

ode = @ODEmodel(
    x1'(t) = -(a01 + a21) * x1(t) + a12 * x2(t) + u(t),
    x2'(t) = a21 * x1(t) - a12 * x2(t) - x3(t) / b,
    x3'(t) = x3(t),
    y(t) = x2(t)
)
assess_identifiability(ode)

mtk_to_si resturns not only an MTK model but also a correspondence between the SI and MTK models. So I think just replacing si_model = mtk_to_si(ode_mtk, measured_quantities) with si_model, _ = mtk_to_si(ode_mtk, measured_quantities) should work

@TorkelE
Copy link
Member Author

TorkelE commented Feb 1, 2024

Ahh, that makes sense. Yes, that worked, thanks a lot :)

@TorkelE
Copy link
Member Author

TorkelE commented Feb 1, 2024

Ok, I have added some tests to ensure that mtk_to_si is exported and works as well. Feel free to merge/ignore/use any part of this PR.

@pogudingleb
Copy link
Collaborator

@TorkelE Thank you very much!

@pogudingleb pogudingleb merged commit 15e2a28 into SciML:master Feb 1, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants